package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import grails.converters.JSON
import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class ComponentController 
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = params.max
        def sort = params['sort']
        def order = params['order']
        def name = params['name']
        def type = params['type']

        def offset = params.offset
        def sql = "  from Component m where 1=1 "
        if (name)
        {
            sql += " and m.name like '%${name}%' "
        }
        if (type)
        {
            sql += " and m.type.id = ${type} "
        }
        def sql1 = " select count(*) " + sql
        if (order && sort)
        {
            sql += " order by m.${sort} ${order}"
        }
        else
        {
            sql += "order by m.modifiedDate desc "
        }

        def componentList = Component.executeQuery(sql, [max: max, offset: offset]).asList()
        def count = Component.executeQuery(sql1)
        respond componentList, model: [componentList: componentList, componentCount: count[0]]
    }

    def show(Component component) {
        respond component
    }

    def create() {
        respond new Component(params)
    }

    @Transactional
    def save(Component component) {
        if (component == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (component.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond component.errors, view:'create'
            return
        }

        component.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'component.label', default: 'Component'), component.id])
                redirect component
            }
            '*' { respond component, [status: CREATED] }
        }
    }

    def edit(Component component) {
        respond component
    }

    @Transactional
    def update(Component component) {
        if (component == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (component.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond component.errors, view:'edit'
            return
        }

        component.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'component.label', default: 'Component'), component.id])
                redirect component
            }
            '*'{ respond component, [status: OK] }
        }
    }

    @Transactional
    def delete(Component component) {

        if (component == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        component.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'component.label', default: 'Component'), component.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'component.label', default: 'Component'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
